package com.jee.ssm.modules.handheldMachine;

import cn.hutool.http.HttpUtil;
import cn.hutool.setting.dialect.Props;
import com.jee.ssm.model.Black;
import com.jee.ssm.model.Order;
import com.jee.ssm.model.json.Tip;
import com.jee.ssm.modules.black.services.BlackService;
import com.jee.ssm.modules.order.services.OrderService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 现金支付
 */
@Controller
@RequestMapping("/handheldNachine/cashPayCode")
public class CashPayCode {

    /**
     * @Description 现金支付
     * @author yanzengbao
     * @date 2019/8/15 上午11:37
     */
    @RequestMapping(value = "/cashPay")
    @ResponseBody
    public Tip cashPay(String orderId){

        System.out.println("------------------现金支付-----------------");
        //查询订单信息
        Order order = orderService.selectById(orderId);
        if(order ==null) return  new Tip(2,"暂未获取到订单信息");

        //查询是否是逃缴订单
        Black black = blackService.selectByOrderId(orderId);

        order.setPayType(3);//现金支付
        order.setOrderStatus(1);
        order.setPayTime(new Date());
        try {

            //修改订单状态
            orderService.updateById(order);

            //删除黑名单记录
            if(black != null) blackService.deleteById(black.getId());

            //更新服务器订单状态
            Map params = new HashMap();
            params.put("id", order.getId());
            params.put("payType",order.getPayType());
            params.put("orderStatus",1);
            Props props = new Props("config/project.properties");
            String serverUrl =props.getProperty("server.url"); //停车服务的地址
            String  method = props.getProperty("tc.updateOrder");
            String str = HttpUtil.get(serverUrl + method, params);
            System.out.println("订单同步(服务器)："+str);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("服务器未链接！");
        }
        return new Tip("现金支付已完成！");
    }






    @Resource
    private OrderService orderService;
    @Resource
    private BlackService blackService;

}
